package com.yourenbang.lingyun.transaction.web.service.impl.ticket;

import com.yourenbang.lingyun.constants.enums.table.AllyTicketEnum;
import com.yourenbang.lingyun.core.message.Result;
import com.yourenbang.lingyun.core.model.vo.PageVO;
import com.yourenbang.lingyun.core.support.PageFrom;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.transaction.mapper.MeScoreAccountMapper;
import com.yourenbang.lingyun.transaction.mapper.TransactionUserMapper;
import com.yourenbang.lingyun.transaction.web.domain.dto.fund.AllyTicketBackstageDTO;
import com.yourenbang.lingyun.transaction.web.domain.dto.fund.AllyTicketDTO;
import com.yourenbang.lingyun.transaction.web.domain.dto.fund.DetailTicketDTO;
import com.yourenbang.lingyun.transaction.web.domain.vo.fund.AllyTicketBackstageVO;
import com.yourenbang.lingyun.transaction.web.domain.vo.fund.AllyTicketVO;
import com.yourenbang.lingyun.transaction.web.domain.vo.fund.DetailTicketVO;
import com.yourenbang.lingyun.transaction.web.service.ticket.TicketAccountService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TicketAccountServiceImpl implements TicketAccountService {

    @Autowired
    private MeScoreAccountMapper meScoreAccountMapper;

    @Autowired
    private TransactionUserMapper transactionUserMapper;

    @Override
    public Result allyTicketYxy(AllyTicketDTO dto) {
        AllyTicketVO allyTicketVO = meScoreAccountMapper.foundAllyTicket(dto);

        Integer refuseAmount = meScoreAccountMapper.foundAllyTicketRefuse(dto.getUserBaseId());
        Integer usedAmount = meScoreAccountMapper.foundAllyTicketUsed(dto.getUserBaseId());
        allyTicketVO.setTicketUsed(usedAmount-refuseAmount);
        return Result.builder().success(allyTicketVO);

    }

    @Override
    public Result detailTicketYxy(DetailTicketDTO dto) {
        List<DetailTicketVO> detailTicketVOS = meScoreAccountMapper.detailAllyTicket(dto);
        detailTicketVOS.forEach(e->{
            if (e.getJournalType().equals(AllyTicketEnum.TicketJournalTypeEnum.RETURN.getCode())) {
                e.setJournalType(AllyTicketEnum.TicketJournalTypeEnum.REWARD.getCode());
            }
        });
        return Result.builder().success(detailTicketVOS);
    }

    @Override
    public Result allyTicketYxyBackstage(AllyTicketBackstageDTO dto) {
        PageFrom.startPage(dto.getPageNum(),dto.getPageSize());
        List<AllyTicketBackstageVO> allyTicketBackstageVOS = transactionUserMapper.getUserBaseId(dto.getUserName(),
                dto.getPhone(), dto.getReferralCode());
        if (CollectionUtils.isEmpty(allyTicketBackstageVOS)) {
            return Result.builder().successPage(allyTicketBackstageVOS);
        }

        allyTicketBackstageVOS.forEach(allyTicketBackstageVO -> {
            AllyTicketDTO allyTicketDTO = new AllyTicketDTO();
            allyTicketDTO.setChannelId(dto.getChannelId());
            allyTicketDTO.setPlatNo(dto.getPlatNo());
            allyTicketDTO.setUserBaseId(allyTicketBackstageVO.getUserBaseId());
            AllyTicketVO allyTicketVO = meScoreAccountMapper.foundAllyTicket(allyTicketDTO);

            allyTicketBackstageVO.setTicketUsed(allyTicketVO.getTicketUsed());
            allyTicketBackstageVO.setTicketIncome(allyTicketVO.getTicketIncome());
            allyTicketBackstageVO.setTicketExpiration(allyTicketVO.getTicketExpiration());
            allyTicketBackstageVO.setTicketUseable(allyTicketVO.getTicketUseable());


        });
        Result pageVOResult = Result.builder().successPage(allyTicketBackstageVOS);
        return pageVOResult;
    }

    @Override
    public Result manageDetailTicketYxy(DetailTicketDTO dto) {
        PageFrom.startPage(dto.getPageNum(),dto.getPageSize());
        List<DetailTicketVO> detailTicketVOS = meScoreAccountMapper.manageDetailAllyTicket(dto);
        Result<PageVO> pageVOResult = Result.builder().successPage(detailTicketVOS);
        return pageVOResult;
    }

    @Override
    public Result allyTicketKlks(AllyTicketDTO dto) {

        KLog.info("考拉快收兑换券账户查询，入参：{}",dto);

        AllyTicketVO allyTicketVO = new AllyTicketVO();
        allyTicketVO.setTicketIncome(meScoreAccountMapper.allyTicketIncome(dto));
        allyTicketVO.setTicketUseable(meScoreAccountMapper.allyTicketUseable(dto));
        allyTicketVO.setTicketExpiration(meScoreAccountMapper.allyTicketExpiration(dto));
        Integer refuseAmount = meScoreAccountMapper.foundAllyTicketRefuseKlks(dto.getUserBaseId());
        Integer usedAmount = meScoreAccountMapper.foundAllyTicketUsedKlks(dto.getUserBaseId());
        allyTicketVO.setTicketUsed(usedAmount-refuseAmount);

        return Result.builder().success(allyTicketVO);

    }

    @Override
    public Result detailTicketKlks(DetailTicketDTO dto) {

        KLog.info("考拉快收兑换券账单明细，入参：{}",dto);
        List<DetailTicketVO> detailTicketVOS = meScoreAccountMapper.detailAllyTicketKlks(dto);

        detailTicketVOS.forEach(detailTicketVO->{
            if (AllyTicketEnum.TicketJournalTypeEnum.RETURN.getCode().equals(detailTicketVO.getJournalType())) {
                detailTicketVO.setJournalType(AllyTicketEnum.TicketJournalTypeEnum.REWARD.getCode());
            }
        });

        return Result.builder().success(detailTicketVOS);
    }

    @Override
    public Result allyTicketKlksBackstage(AllyTicketBackstageDTO dto) {

        KLog.info("考拉快收兑换券后台查询，入参：{}",dto);
        PageFrom.startPage(dto.getPageNum(),dto.getPageSize());
        List<AllyTicketBackstageVO> allyTicketBackstageVOS = transactionUserMapper.getUserBaseId(dto.getUserName(),
                dto.getPhone(), dto.getReferralCode());
        if (CollectionUtils.isEmpty(allyTicketBackstageVOS)) {
            return Result.builder().successPage(allyTicketBackstageVOS);
        }

        allyTicketBackstageVOS.forEach(allyTicketBackstageVO -> {

            Integer refuseAmount = meScoreAccountMapper.foundAllyTicketRefuseKlks(allyTicketBackstageVO.getUserBaseId());
            Integer usedAmount = meScoreAccountMapper.foundAllyTicketUsedKlks(allyTicketBackstageVO.getUserBaseId());
            allyTicketBackstageVO.setTicketUsed(usedAmount-refuseAmount);

            AllyTicketDTO allyTicketDTO = new AllyTicketDTO();
            allyTicketDTO.setChannelId(dto.getChannelId());
            allyTicketDTO.setPlatNo(dto.getPlatNo());
            allyTicketDTO.setUserBaseId(allyTicketBackstageVO.getUserBaseId());

            allyTicketBackstageVO.setTicketIncome(meScoreAccountMapper.allyTicketIncome(allyTicketDTO));
            allyTicketBackstageVO.setTicketExpiration(meScoreAccountMapper.allyTicketExpiration(allyTicketDTO));
            allyTicketBackstageVO.setTicketUseable(meScoreAccountMapper.allyTicketUseable(allyTicketDTO));
        });

        return Result.builder().successPage(allyTicketBackstageVOS);
    }

    @Override
    public Result manageDetailTicketKlks(DetailTicketDTO dto) {

        KLog.info("考拉快收兑换券明细后台查询，入参：{}",dto);

        PageFrom.startPage(dto.getPageNum(),dto.getPageSize());
        List<DetailTicketVO> detailTicketVOS = meScoreAccountMapper.manageDetailAllyTicketKlks(dto);

        return Result.builder().successPage(detailTicketVOS);
    }
}
